package cn.edu.glut.choosethesis.service.impl;


import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.assertj.core.util.Preconditions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.glut.choosethesis.repository.SelectionRepository;
import cn.edu.glut.choosethesis.repository.SubjectRepository;
import cn.edu.glut.choosethesis.service.SelectionService;
import cn.edu.glut.entity.Selection;
import cn.edu.glut.entity.Student;
import cn.edu.glut.entity.Subject;
import cn.edu.glut.exception.ServiceException;
import cn.edu.glut.user.repository.StudentRepository;
import cn.edu.glut.vo.SelectionToStudentVO;
/**
 * 
 * @author Caizhfy
 * @version v.0.1
 * @createTime 2017年12月21日下午1:56:04
 * @email Caizhfy@163.com
 * @description
 *
 */
@Service
public class SelectionServiceImpl implements SelectionService{
	@Autowired
	SelectionRepository selectionRepository;
	@Autowired
	SubjectRepository subjectRepository;
	@Autowired
	StudentRepository studentRepository;

	//选题操作，关联外键，修改其他操作
	//由于未查出主键，所以不能使用ORM的形式
	@Transactional
	@Override
	public int updateSelectStatus(String studentId, String subjectId) {
		//安全起见，先判断该题目是不是该老师的题目
		Preconditions.checkNotNull(studentRepository.findOne(studentId), "该学生不存在");
		//查询该题目是否已经被确选过了
		int isChoice = selectionRepository.findMaxIsSelectBySubjectId(subjectId);//比1小才算是没有被选,等于1就是被选了
		if(isChoice == 1)
			throw new ServiceException("该题目已经被确选");
		//修改主状态为确选
		selectionRepository.updateIsSelectByStudentIdAndSubjectId(SelectionService.CHOICE_SUCCESS,studentId, subjectId, new Date(System.currentTimeMillis()));
		//修改其他的记录状态为落选
		List<Selection> list = selectionRepository.findAllBySubjectIdAndNotIsStudentId(subjectId, studentId);
		for(Selection s : list){
			s.setIsSelection(SelectionService.CHOICE_FAILURE);
		}
		//持久化落选状态(批处理)
		selectionRepository.save(list);
		return 1;
	}

	
	
	// 学生选题--待优化，减少sql发送数量
	@Override
	@Transactional
	public int studentChoiceSubject(String studentId, String subjectId, int wish) {
		// 判断该学生选题总数是否超过两个

		if (selectionRepository.countByStudentId(studentId) > 1)
			throw new ServiceException("选题数量不能超过2个");
		// 判断该学生是否已经选过该题目
		if (selectionRepository.countByStudentIdAndSubjectId(studentId, subjectId) == 1)
			throw new ServiceException("请勿重复选题");
		// 判断某个学生是否已经确选了题目
		if(selectionRepository.findMaxIsSelectByStudentId(studentId)==1)
			throw new ServiceException("该学生已经确选了题目");
		// 判断题目是否已经被确选了
		if (selectionRepository.findMaxIsSelectBySubjectId(subjectId)==1)// 比1小才算是没有被选,等于1就是被选了
			throw new ServiceException("该题目已经被确选");
		// 选题操作
		// 先修改subject表的number字段
		Subject subject = subjectRepository.findOne(subjectId);
		Student student = studentRepository.findOne(studentId);
		//判断题目是否还有剩余量
		if (subject == null || StringUtils.isEmpty(subject.getId()) || subject.getNumber() == null
				|| subject.getNumber() == 0)
			throw new ServiceException("该题目的可选剩余量已经为0");
		
		int number = subject.getNumber();
		number--;
		subject.setNumber(number);
		subjectRepository.save(subject);
		// 修改中间表
		Selection se = selectionRepository
				.save(new Selection(subject, student, wish, SelectionService.NO_CHOICE));
		if (null != se && !StringUtils.isEmpty(se.getId()))
			return 1;
		return 0;
	}

	@Override
	public List<SelectionToStudentVO> findByStudentId(String studentId) {
		return selectionRepository.findByStudentId(studentId);
	}
	
	//退选
	@Override
	@Transactional
	public int refund(String studentId, String subjectId) {
		Selection se = selectionRepository.findByStudentIdAndSubjectId(studentId, subjectId);
		Preconditions.checkNotNull(se, "你并没有选该题目");
		//如果该题目是确选的题目，不能退选
		Integer isSelect = selectionRepository.findMaxIsSelectBySubjectId(subjectId);
		if(isSelect==1)
			throw new ServiceException("该题目已经被确选, 不能退选");
		Subject s = subjectRepository.findOne(subjectId);
		Preconditions.checkNotNull(s, "该记录不存在,请勿非法传入数据...");
		int number = s.getNumber();
		s.setNumber(++number);
		subjectRepository.save(s);
		// 直接删除中间表
		selectionRepository.deleteByStudentIdAndSubjectId(studentId, subjectId);
		return 1;
	}



	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public int privacyChoice(String username, String subjectId) {
		Student s = studentRepository.findByUsername(username);
		Preconditions.checkNotNull(s, "该学生不存在");
		studentChoiceSubject(s.getId(), subjectId, 1);
		updateSelectStatus(s.getId(), subjectId);
		return 1;
	}
	
}
